<template>
  <n-form
      style="margin-top: 24px"
      ref="formRef"
      :model="form"
      :rules="rules"
      label-placement="top"
      label-width="120"
      require-mark-placement="right-hanging"
  >

    <n-form-item label="参数名" path="paramName">
      <n-input :disabled="true" v-model:value="form.paramName" placeholder="请输入参数名"></n-input>
    </n-form-item>
    <n-form-item  label="参数值类型" path="type">
      <n-input :disabled="true" v-model:value="form.type" placeholder="请输入参赛值类型"></n-input>
    </n-form-item>
    <n-form-item label="参数描述" path="briefInfo">
      <n-input v-model:value="form.briefInfo" type="textarea" style="min-height: 200px"
               placeholder="请输入参数描述"></n-input>
    </n-form-item>
    <n-form-item label="是否必填">
      <n-radio-group v-model:value="form.required" name="radiogroup">
        <n-space>
          <n-radio :value="true">
            是
          </n-radio>
          <n-radio :value="false">
            否
          </n-radio>
        </n-space>
      </n-radio-group>
    </n-form-item>
    <n-form-item label="默认值">
      <n-input v-model:value="form.defaultValue" placeholder="请输入所属类名方法名"></n-input>
    </n-form-item>


  </n-form>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {JBoltApi} from '@/service/request'

const props = defineProps({
  id: {
    type: String
  },
  functionId: {
    type: String
  }
})


const formRef = ref(null)

const form = ref({
  id:'',
  aiFunctionId: props.functionId,
  paramName: "",
  type: "",
  briefInfo: "",
  defaultValue: "",
  sortRank: 0,
  createTime: "",
  required: true
})

const rules = {
  paramName: {
    required: true,
    message: '请输入参数名',
    trigger: 'change'
  },
  type: {
    required: true,
    message: '请输入参数值类型',
    trigger: 'change'
  },
  briefInfo : {
    required: true,
    message: '请输入参数值类型',
    trigger: 'change'
  }
}

async function submit(fun) {
  await formRef.value?.validate()

  console.debug('提交表单', form.value)

  let url = props.id ? '/aiFunctionParam/update' : '/aiFunctionParam/add'

  await JBoltApi.tryPost(url, form.value)


  if (fun) {
    fun()
  }

  return true
}

defineExpose({
  submit
})

function loadEditData(){
  JBoltApi.tryGet(`/aiFunctionParam/getById/${props.id}`).then(res => {
    form.value = res.data
  }).catch((error)=>{
      console.error(error)
  })
}

onMounted(()=>{
  if(props.id){
    loadEditData()
  }
})


</script>

<style scoped lang="scss">
</style>